﻿using EmployeeLeaveSystem.Shared.DAO;
using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EmployeeLeaveSystem.Server.Controllers {
    [ApiController]
    [Route("[Controller]/[Action]")]
    public class MainController : ControllerBase {
        readonly EpMainContext _context;
        public MainController(EpMainContext context) {
            _context = context;
        }
        // -------请假信息表格--------
        // 增加一条请假消息，压入数据库中
        [HttpPost]
        public NetworkData AddLeaveDetail(LeaveDetail _data) {
            NetworkData res = new();
            try {
                var cache = _context.LeaveDetailInfos.Where(x => x.StaffId == _data.StaffId).OrderByDescending(x => x.Id).FirstOrDefault();
                if (cache != null && cache.AcceptFlag != AcceptFlag.正常结束) { // 检查能否递交的资格，如果最后一个递交请假还未结束，那么就不能申请
                    res.errCode = 502;
                    res.errMsg = "拒绝递交此次请假申请，因为还有进行中的假还未能结束，可以在【我的历史】中查看。";
                    return res;
                }
                _data.LeaveDate = DateTime.Now;
                _data.AcceptFlag = AcceptFlag.审核中;
                _context.Add(_data);
                _context.SaveChanges();
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 502;
            }
            return res;
        }
        // 通过UserID获得历史记录
        [HttpGet]
        public NetworkData GetHistoryByUserId(int id) {
            NetworkData res = new();
            try {
                var data = _context.StaffInfos.Where(x => x.Id == id).Include(x => x.LeaveDetail).ThenInclude(x => x.AcceptStaff).FirstOrDefault();
                res.data = data;
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 502;
            }
            return res;
        }

        // 删除一条请假消息，在数据库之中，通过ID进行删除
        [HttpGet]
        public NetworkData RemoveLeaveDetailById(int id) {
            NetworkData res = new();
            try {
                var data = _context.LeaveDetailInfos.Where(x => x.Id == id).First();
                _context.Remove(data);
                _context.SaveChanges();
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 502;
            }
            return res;
        }
        // 获得所有的请假信息
        [HttpGet]
        public NetworkData GetAllLeavesData() {
            NetworkData res = new();
            try {
                res.data = _context.LeaveDetailInfos.OrderBy(x => x.Id).ToList();
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 502;
            }
            return res;
        }
        [HttpPost]
        public NetworkData Login(StaffInfo staff) {
            NetworkData res = new();
            try {
                var st = _context.StaffInfos.Where(x => x.Name == staff.Name).Include(t => t.Department).FirstOrDefault();
                if (st == null) {
                    res.errMsg = "名称不存在";
                    res.errCode = 201;
                } else {
                    if (st.Password == staff.Password) {
                        res.errMsg = "登录成功";
                        res.data = st;
                        res.errCode = 200;
                    } else {
                        res.errMsg = "密码错误";
                        res.errCode = 201;
                    }
                }
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 503;
            }
            return res;
        }
        [HttpGet]
        [HttpPost]
        public NetworkData GetAllStaffInfos() { // 包含部门的信息，但是不包含上司的信息
            NetworkData res = new();
            try {
                res.data = _context.StaffInfos.Include(x => x.Department).ToList();
            }
            catch (Exception ex) {
                res.errMsg = ex.Message;
                res.errCode = 503;
            }
            return res;
        }
        // 通过LeavesID来获得所有的评价记录
        [HttpGet]
        public List<Comments> GetCommentsByLeavesId(int id) {
            return _context.Comments.Where(x => x.LeaveDetailId == id).OrderBy(x => x.Time).ToList();
        }
        [HttpGet]
        public List<Comments> GetCommentsWithStaffInfoByLeavesId(int id) {
            return _context.Comments.Where(x => x.LeaveDetailId == id).OrderBy(x => x.Time).Include(x => x.StaffInfo).ToList();
        }
        // 通过ID来获得用户的相关信息
        [HttpGet]
        public StaffInfo? GetStaffInfoById(int id) {
            return _context.StaffInfos.Where(x => x.Id == id).Include(x => x.Department).FirstOrDefault();
        }
        [HttpPost]
        public void PostComment(Comments cms) {
            _context.Comments.Add(cms);
            _context.SaveChanges();
        }
        [HttpPost]
        public void UpdateLeaveDetailAcceptFlag(UpdateLeaveAcceptFlag data) {
            var o = _context.LeaveDetailInfos.Where(x => x.Id == data.Id).FirstOrDefault();
            Console.WriteLine(o?.Id + " " + o?.AcceptFlag);
            if (o != null) {
                o.AcceptFlag = data.AcceptFlag;
                _context.Update(o);
                _context.SaveChanges();
            }
        }
        [HttpGet]
        public LeaveDetail? GetDetaisById(int id) {
            return _context.LeaveDetailInfos.Where(x => x.Id == id).FirstOrDefault();
        }
        [HttpGet]
        public List<LeaveDetail> GetAcceptLeaveDetaiByUsrId(int id) {
            return _context.LeaveDetailInfos.Where(x => x.AcceptStaffId == id).ToList();
        }
    }
}
